import unittest, requests, pymysql
from db import DateHandle

from ddt import ddt, file_data, data, unpack
from common import read_data_from_csv

data_csv = read_data_from_csv('../login.csv')


@ddt()
class A3mall(unittest.TestCase):

    def setUp(self) -> None:
        print('开始测试')
        # 连接数据库
        self.db = DateHandle('10.59.9.14', 'root', 'yidong666', 'a3mall')

    def tearDown(self) -> None:
        print('测试结束')
        # 关闭连接
        self.db.close()

    # 登录
    # 上游接口的响应中动态的响应数据做为下游接口的请求头或请求体入参
    token = None
    group_name = None
    username = None
    price_all = None
    login_id = None
    @data(*data_csv)     # *解包--将data_csv最外层的[]打开
    @unpack      # 解包--将data_csv内层的[]打开
    def test_A3mall_01_login(self,name,password,casename,expected):
        scrpit_login = requests.post(url='http://10.59.9.14:7003/api/public/login',
                                     json={"username": name, "password": password})
        print(scrpit_login.json())
        if scrpit_login.json()['status'] == 1:
            A3mall.token = scrpit_login.json()['data']['token']
            # 断言
            A3mall.group_name = scrpit_login.json()['data']['group_name']
            self.assertIn('会员', A3mall.group_name)
            A3mall.username = scrpit_login.json()['data']['username']
            # self.assertEqual('小高4', A3mall.username)
            # 余额
            A3mall.price_all = scrpit_login.json()['data']['amount']
            print(A3mall.price_all)
            # 用户id
            A3mall.login_id = scrpit_login.json()['data']['id']
            print(A3mall.login_id)

    # 签到
    def test_A3mall_02_send(self):
        a3mall_send = requests.get(url='http://10.59.9.14:7003/api/sign/send',
                                   headers={'auth-token': 'Bearer ' + str(A3mall.token)})
        print(a3mall_send.json())

    # 搜索
    def test_A3mall_03(self):
        r3 = requests.get(url='http://10.59.9.14:7003/api/search')
        print(r3.json())

    # 商品列表
    spid = None
    def test_A3mall_04(self):
        r4 = requests.get(url='http://10.59.9.14:7003/api/search/list?page=1&keywords=%E6%B5%8B%E8%AF%95&type=0&sort=1')
        print(r4.json())
        A3mall.spid = r4.json()['data']['list'][0]['id']

    # 商品详情
    title = None

    def test_A3mall_05(self):
        r5 = requests.get(url='http://10.59.9.14:7003/api/goods/view?id=' + str(A3mall.spid))
        print(r5.json())
        A3mall.title = r5.json()['data']['goods']['title']
        print(A3mall.title)
        self.assertEqual('测试机003', A3mall.title)

    # 加入购物车
    def test_A3mall_06(self):
        r6 = requests.post(url='http://10.59.9.14:7003/api/cart/add',
                           json={"id": str(A3mall.spid), "sku_id": "", "num": 1},
                           headers={'auth-token': 'Bearer ' + str(A3mall.token)})
        print(r6.json())

    addid = None

    # 购物车列表
    # 商品总库存
    nums = None
    def test_A3mall_07(self):
        r7 = requests.get(url='http://10.59.9.14:7003/api/cart?page=1',
                          headers={'auth-token': 'Bearer ' + A3mall.token})
        print(r7.json())
        A3mall.addid = r7.json()['data']['list'][0]['id']
        print(A3mall.addid)
        # 商品总库存
        A3mall.nums = r7.json()['data']['list'][0]['nums']
        print('nums:'+str(A3mall.nums))

    # 提交订单
    order_price = None
    # 购买商品的数量
    goods_nums = None
    def test_A3mall_08(self):
        r8 = requests.get(
            url='http://10.59.9.14:7003/api/order/confirm?id=' + str(A3mall.addid) + '&type=cart&shipping_type=1',
            headers={'auth-token': 'Bearer ' + str(A3mall.token)})
        print(r8.json())
        A3mall.order_price = r8.json()['data']['order_amount']
        print(A3mall.order_price)
        # 数据库断言
        point_ye = self.db.select(f"select amount from mall_users where id = '{A3mall.login_id}'")[0][0]
        print(type(point_ye))
        print(point_ye)
        # 购买商品的数量
        A3mall.goods_nums = r8.json()['data']['item'][0]['goods_nums']
        print('goods_nums'+str(A3mall.goods_nums))

        

    # 创建订单
    order_id = None
    def test_A3mall_09(self):
        r9 = requests.post(url='http://10.59.9.14:7003/api/order/create',
                           headers={'auth-token': 'Bearer ' + A3mall.token},
                           json={"id": str(A3mall.addid), "type": "cart", "address_id": 45, "store_id": 1,
                                 "shipping_type": 1, "bonus_id": "0", "payment": "balance", "remarks": "", "source": 1,
                                 "url": "http://10.59.9.14:7003/wap/pages/cart/confirm?id=8&type=cart"})
        print(r9.json())
        A3mall.order_id = r9.json()['data']['order_id']

    # 订单详情
    order_no = None
    title_cs = None
    def test_A3mall_10(self):
        r10 = requests.post(url='http://10.59.9.14:7003/api/order/detail',
                            headers={'auth-token': 'Bearer ' + str(A3mall.token)},
                            json={"id": A3mall.order_id})
        print(r10.json())
        A3mall.order_no = r10.json()['data']['order_no']
        print(A3mall.order_no)
        A3mall.title_cs = r10.json()['data']['item'][0]['title']
        print(A3mall.title_cs)
        # 数据库断言
        db_order_no = self.db.select(f"select order_no from mall_order where id = '{A3mall.order_id}'")
        self.assertEqual(db_order_no[0][0], A3mall.order_no)
        print('查询成功')
        print(db_order_no[0][0])
        # 断言库存
        store_nums = self.db.select(f"select store_nums from mall_goods where title = '{A3mall.title_cs}'")
        print('剩余的库存：'+str(store_nums[0][0]))
        print('总库存：' + str(A3mall.nums))
        print('商品购买的数量：'+str(A3mall.goods_nums))
        self.assertEqual(int(A3mall.nums)-int(A3mall.goods_nums), int(store_nums[0][0]))




    # 个人信息
    amount_ye = None
    def test_A3mall_11(self):
        r11 = requests.get(url='http://10.59.9.14:7003/api/ucenter/info',headers={'auth-token': 'Bearer ' + str(A3mall.token)})
        print(r11.json())
        A3mall.amount_ye = float(r11.json()['data']['amount'])
        print(A3mall.amount_ye)
        point_ye = self.db.select(f"select amount from mall_users where id = '{A3mall.login_id}'")[0][0]
        print(type(point_ye))
        print(point_ye)
        self.assertEqual(float(A3mall.price_all)-float(A3mall.order_price), float(point_ye))





if __name__ == '__main__':
    unittest.main()
